var nbTabItem = 1;
var couleurDefaut = 'red';
var voletOuvert = 0
var canSave =  0;
var currentTab = '';
var onLoad = false;
var currentItemLoad = 0;
var idFilter = 0
var idNetwork = 0
var indexNumber = 0;
$.extend({
	URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
 while(x<c.length){var m=r.exec(c.substr(x));
   if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
   }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
   o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
 while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
 t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
function refreshTree() {
 setTimeout(getListMap, 5);
 setTimeout(getListDiffConf, 10);
}
function getListMap() {
 $.post(baseUrl + "/creation/listmap/", {}, listMapTree, "json")
}
function listMapTree(a) {
var maptree = $("#divMapTree");
 maptree.find("*").remove();
 maptree.append('<ul id="browserMap" class="filetree treeview-famfamfam">');
 maptree = $("#browserMap");
 var b = "";
 if(a.data.length == 0) {
   b = '<li><span style="margin-left: 5px">&#216;</span></li>'
 }else {
   for(var c = 0;c < a.data.length;c++) {
     if(a.data[c].LblConfiguration.length > 0) {
       b += '<li><span class="map" OnClick="visualisationTopologie(\'' + a.data[c].LblConfiguration + '\')"><span style="margin-left: 5px">' + a.data[c].LblConfiguration + "</span></span></li>"
     }
   }
 }
 b += "</ul>";
 maptree.append(b);
 maptree.treeview({collapsed:true})
}
function getListDiffConf() {
 $.post(baseUrl + "/config/getlistdiffconf/", {}, listConfTree, "json")
}
function listConfTree(a) {
 var conftree = $("#divConfTree");
 conftree.find("*").remove();
 conftree.append('<ul id="browserConf" class="filetree treeview-adn">');
 conftree = $("#browserConf");
 var b = "";
 if(a.comp == false) {
   b += '<li><span style="margin-left: 5px">&#216;</span></li></ul>'
 }else {
   for(var c = 0;c < a.comp.length;c++) {
     var e = a.comp[c].split('-');
     b += '<li><span class="Folder" title="' + e[1] + '">' + e[0] + '</span><ul>';
     for(var d = 0;d < a.conf.length;d++) {
       if(a.conf[d][e[0]]) {
        var arr = a.conf[d][e[0]].split(";");
         b += '<li><span class="config" idconf="' + arr[0] + '"><span style="margin-left: 5px">' + arr[1] + "</span></span></li>"
       }
     }
     b += "</li></ul>"
   }
 }
 b += "</ul>";
 conftree.append(b);
 conftree.treeview({collapsed:true});
 $(".config").click(function() {
   ajouterItem("Configuration", "config/");
   setTimeout(getConfDiff, 2000 ,this)
 })
configToolTip();
}
function configToolTip() {
  $("#browserConf li span[title]").tooltip({delay:0, track:true, fade:250})
}
function addLoad() {
 $("#loadIcon").append('<img class="loader" border="0" alt="" src="' + baseUrl + '/public/img/base/load.gif">');
 return true
}
function cleanLoad() {
 $("#loadIcon").find("*").remove();
 return true
}
function attenteReponse(a) {
$(".loader").length == 0 && addLoad();
if(timers.tab[a.functionReturn][3] == undefined) {
   timers.tab[a.functionReturn][3] = 0
 } 
 if(timers.tab[a.functionReturn][2] == timers.tab[a.functionReturn][3]) {
     cleanLoad();
     timers.tab[a.functionReturn][3] = null
     alertGrowl(MISC_ERROR, MISC_SONDE_ERROR)
     return false;
 }
 if(donneeARelancer(a)) {
  var b = setTimeout(function() {
     postFuncReturn(a.functionReturn, a.donnees)
   }, timers.tab[a.functionReturn][1] * 100);
   timers.tab[a.functionReturn][3]++;
   timers.tab[a.functionReturn][4] = b;
   return true
 }
 cleanLoad();
 timers.tab[a.functionReturn][3] = null;
 timers.tab[a.functionReturn][0](a.donnees);
 return false
}
function postFuncReturn(a,b) {
 return $.post(baseUrl + a, {donnees:b}, attenteReponse, "json")
}
function donneeARelancer(a) {
 if(a == null) {
   return true
 }
 if(a.value == true || a.rechercheTermine != null && a.rechercheTermine.termine == false) {
   return true
 }
 return false
}
function selectFilterBaseFiltre(a) {
 var c = [], d = $("#listFiltreLog");
 d.empty();
 if(a.filtre.length == undefined) {
   c[0] = "<option value='-'>-</option>"
 }
 for(var b = 0, f = a.filtre.length;b < f; b++) {
   c[b] = "<option value='" + a.filtre[b].NameFilter + "'>" + a.filtre[b].NameFilter + "</option>"
 }
 d.append(c.join(""));
 selectFilterBaseModel(a)
}
function selectFilterBaseModel(a) {
 var d = $("#listModelLog"), g = $("#listModelBackup, #listModelRestore, #listModelCommand");
 d.empty();
 g.empty();
 for(var c = [], b = 0, f = a.model.length; b < f; b++) {
   if($(d + "option[value=" + a.model[b] + "]").length == 0) {
     c[b] = "<option value='" + a.model[b] + "'>" + a.model[b] + "</option>"
   }
 }
 d.append(c.join(""));
 g.append(c.join(""));
 selectFilterBaseType(a)
}
function selectFilterBaseType(a) {
var d = $("#listTypeLog"), g = $("#listTypeBackup, #listTypeRestore, #listTypeCommand");
d.empty();
g.empty();
for(var c = [], b = 0,f = a.type.length;b < f; b++) {
   if($(d + "option[value=" + a.type[b] + "]").length == 0) {
     c[b] = "<option value='" + a.type[b] + "'>" + a.type[b] + "</option>"
   }
 }
 d.append(c.join(""));
 g.append(c.join(""));
 selectFilterBaseProto(a)
}
function selectFilterBaseProto(a) {
var d = $("#listProtocolLog"), g = $("#listProtocolBackup, #listProtocolRestore, #listProtocolCommand");
d.empty();
g.empty();
 for(var c = [], b = 0, f = a.protocol.length;b < f; b++) {
   if($(d + "option[value=" + a.protocol[b] + "]").length == 0) {
     c[b] = "<option value='" + a.protocol[b] + "'>" + a.protocol[b] + "</option>"
   }
 }
 d.append(c.join(""));
 g.append(c.join(""));
 selectFilterBaseUser(a);
}
function selectFilterBaseUser(a) {
 var c = [], d = $("#listUserLog"), b = $("#listUserBackup, #listUserRestore, #listUserCommand");
 d.empty();
 b.empty()
 if(a.identity == false) {
   c[0] = "<option value='-'>-</option>";
 }
 for(var e = 0, f = a.identity.length;e < f;e++) {
     c.push("<option value='" + a.identity[e].LoginNetwork + "'>" + a.identity[e].NameNetwork + "</option>");
 }
 d.append(c.join(""));
 b.append(c.join(""));
}
function selectRestoreBaseConf(a, f) {
 var c = [], d = $("#listConfRestore"), e = $("#listConfDiff");
 d.empty();
 e.empty();
 if(a.conf.length == 0 || a.conf.length == undefined) {
   c[0] = "<option value='-'>-</option>"
 }
 for(var b = 0, g = a.conf.length; b < g; b++) {
   c[b] = "<option value='" + a.conf[b].IdConfiguration + "'>" + a.conf[b].Date + "</option>"
 }
 $(d).append(c.join(""));
 $(e).append(c.join(""));
 if (typeof f == 'undefined')
   selectFilterBaseModel(a)
}
function ajouterItem(b, a) {
 if(b != null && a != null && b != "" && a != "") {
   tabOuverte[a] == null ? $(function() {
     $("#tabs").tabs("add", baseUrl + "/" + a, b)
   }) : $("#tabs").tabs("select", "#" + tabOuverte[a].id)
 }
}
function deleteOnglet(f, b) {
 $(function() {
   var c = $(".volet");
   var a = b.helper.context.lastElementChild.lastElementChild.defaultValue;
   a = a.substring(baseUrl.length + 1);
   alert("URL" + a);
   var d = tabOuverte[a].index, e = b.helper.context.textContent;
   $("#tabs").tabs("remove", d);
   $("ul", c).length ? $("ul", c) : $('<ul class="gallery ui-helper-reset"/>').appendTo(c);
   $(".volet").append("<li><div class='voletItem' style='list-style:none;'>" + e + "<input type='hidden' value='" + a + "'/></div></li>").fadeIn(function() {
   })
 })
}
function validWindows(b,c) {
 var a = $("#dialogValidWindows");
 a.dialog("destroy");
 var btns = {};
 btns[BUTTON_NO] = function() {$(this).dialog("destroy")};
 btns[BUTTON_YES] =function() {$(this).dialog("destroy"); valid.tab[b](c)};
 a.dialog({autoOpen:false, draggable:false, modal:true, buttons:btns});
 a.dialog("open");
}
function alertGrowl(a, b) {
 $.gritter.add({title:a, text:b})
}
function showFilled(a) {
   return a > 9 ? "" + a : "0" + a
}
function ouvrirFermerVolet() {
 if(voletOuvert == 0) {
   voletOuvert = 1;
   $(function() {
     $(".volet").animate({width:"0px"}, 1E3);
     $(".volet_fleche_deplie").style.display = "none";
     $(".volet_fleche_plie").style.display = ""
   })
 }else {
   voletOuvert = 0;
   $(function() {
     $(".volet").animate({width:"120px"}, 1E3);
     $(".volet_fleche_deplie").style.display = "";
     $(".volet_fleche_plie").style.display = "none"
   })
 }
}
function sauverTabInterface(d, a, b) {
 a = $.URLEncode(a.replace(RegExp("/", "g"), "-"));
 b = $.URLEncode(b.replace(RegExp("<span>|</span>", "g"), ""));
 if(d == "add") {
   $.ajax({type:"POST", url:baseUrl + "/Interface/ajoutertab/url/" + a + "/label/" + b, success:function(c) {
   }})
 }else {
   d == "delete" && $.ajax({type:"POST", url:baseUrl + "/Interface/retirertab/url/" + a, success:function(c) {
   }})
 }
}
function getUrlFromUi(a) {
 if(a.tab.parentNode.nextElementSibling != null) {
   return a.tab.parentNode.nextElementSibling.defaultValue
 }else {
   if(a.tab.parentNode.lastElementSibling != null) {
     return a.tab.parentNode.lastElementSibling.defaultValue
   }
 }
 return""
}
function initTab() {
 $tabs = $("#tabs").tabs({tabTemplate:'<li class="dragItem ui.draggable"><div><i><a href="#{href}"><span>#{label}</span></a></i><input type="hidden" value="#{href}"/></div></li>',
   cache:true, add:function(c, b) {
   $tabs.tabs("select", "#" + b.panel.id);
   var a = b.tab.parentNode.nextElementSibling.defaultValue;
   a = a.substring(baseUrl.length + 1);
   var d = [];
   d.id = b.panel.id;
   d.index = b.index;
   tabOuverte[a] = d;
   $(".dragItem").draggable({helper:"clone"});
   currentTab = a;
   canSave = 1
 }, load:function(c, b) {
   if(canSave == 1) {
     sauverTabInterface("add", currentTab, b.tab.innerHTML);
     canSave = 0;
     onLoad && loadInterface()
   }
 }, remove:function(c, b) {
   var a = getUrlFromUi(b);
   if(a != "") {
     a = a.substring(baseUrl.length + 1);
     delete tabOuverte[a];
     sauverTabInterface("delete", a, "")
   }
 }, select:function(c, b) {
   var a = getUrlFromUi(b);
   selectToolbar(a, b.index)
 }});
 $tabs.tabs("paging", {cycle:true, follow:true, followOnSelect:true, selectOnAdd:true});
 $("#remove").click(function() {
   var c = $tabs.tabs("option", "selected");
   c != 0 && $("#tabs").tabs("remove", c)
 })
}
function initCornerRadius() {
   $("#tabs").removeClass("ui-corner-all").addClass("ui-corner-right").css("border","0");
   $("#corp").addClass("ui-corner-all");
   $("#corp_right").addClass("ui-corner-right");
   $("#corp_left_top").addClass("ui-corner-tl");
   $("#corp_left_bottom").addClass("ui-corner-bl");
   $("#top_left").addClass("ui-corner-left");
   $("#top_right").addClass("ui-corner-right");
   $("#top_date").addClass("ui-corner-bottom");
   $("#footer_left").addClass("ui-corner-left");
   $("#footer_right").addClass("ui-corner-right");
}
function initOpacity() {
   $("#top_middle").css("z-index", 1000);
   $(".over, #top_left, #top_middle, #top_right, #footer_left, #footer_right, #corp_left_top, #corp_left_bottom").each(function() {
       $(this).css("opacity", "0.97");
   });
}
function loadInterface() {
 $.getJSON(baseUrl + "/Interface/affichertab/", function(a) {
   if(a.tab[currentItemLoad] != null) {
     var b = $.URLDecode(a.tab[currentItemLoad].item.url).replace(RegExp("-", "g"), "/");
     onLoad = true;
     ajouterItem(a.tab[currentItemLoad].item.label, b)
     //setTimeout(ajouterItem, 5, a.tab[currentItemLoad].item.label, b)
     currentItemLoad++
    }else {
     onLoad = false
   }
 })
}
function loadToolbar(a) {
 a = a.replace(RegExp("/", "g"), "-");
 $.ajax({type:"POST", url:baseUrl + "/toolbar/index/source/" + a, success:function(b) {
   $("#toolbar > div").html(b)
 }})
}
function selectToolbar(a, b) {
 if(b != 0) {
   a = a.substring(baseUrl.length + 1);
   toolbar[a] != null ? $("#toolbar > div").html(toolbar[a]) : loadToolbar(a)
 }else {
   $("#toolbar > div").html("")
 }
}
function navigationToolbar(b, c) {
 $.ajax({type:"POST", url:baseUrl + "/" + c, success:function(a) {
   a.trim() == "" ? alertGrowl(DASHBOARD_CONFIG_TXT, DASHBOARD_LOAD_ERROR) : $("#" + b).html(a)
 }})
}
function refreshDashboard() {
	$("#tabs").tabs("load", 0);
}
$(function() {
   initTab();
   $("#tabs > ul").after("<div id='toolbar'><div></div></div>");
   initCornerRadius();
   setTimeout(loadInterface,10);
   $("#accordion").accordion({
       autoHeight: false,
       navigation: true,
       collapsible: true
   });
  $(".dragItem").draggable({
       helper: 'clone',
      drag: function(event, ui) {
      }
   });
    refreshTree();
    timerDash.add(refreshDashboard, 30000, null);
});




